﻿@page "/Axes/ColorsAndPosition"
@using LiveChartsCore.SkiaSharpView.Blazor
@using LiveChartsCore;
@using LiveChartsCore.SkiaSharpView;
@using LiveChartsCore.Measure;
@using LiveChartsCore.SkiaSharpView.Painting;
@using SkiaSharp;

<div>
    <button type="button" class="btn btn-primary" @onclick="SetNewColor">Set new color</button>
    <button type="button" class="btn btn-primary" @onclick="TogglePosition">Toggle position</button>
</div>

<CartesianChart
    Series="@series"
    XAxes="@xAxes"
    YAxes="@yAxes">
</CartesianChart>

@code {
    private ISeries[] series = new ISeries[]
    {
        new ColumnSeries<double> { Values = new double[] { 2, 3, 8 } }
    };

    private Axis xAxis;
    private Axis yAxis;
    private Axis[] xAxes;
    private Axis[] yAxes;
    private Random random = new Random();

    public ColorsAndPosition()
    {
        xAxis = new Axis
        {
            Position = AxisPosition.End,
            Name = "X Axis",
            LabelsPaint = new SolidColorPaint(SKColors.Red)
        };
        yAxis = new Axis
        {
            Name = "Y Axis"
        };
        xAxes = new[] { xAxis };
        yAxes = new[] { yAxis };
    }

    private void TogglePosition()
    {
        xAxis.Position = xAxis.Position == AxisPosition.End ? AxisPosition.Start : AxisPosition.End;
    }

    private void SetNewColor()
    {
        var r = (byte)random.Next(0, 255);
        var g = (byte)random.Next(0, 255);
        var b = (byte)random.Next(0, 255);
        xAxis.LabelsPaint = new SolidColorPaint(new SKColor(r, g, b));
    }
}
